package com.chandra.pos.order.dao;

import com.chandra.pos.order.dao.entity.Order;
import com.chandra.pos.order.model.OrderStatus;
import com.chandra.pos.order.model.ShippingStatus;
import com.chandra.pos.order.repository.OrderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;

import javax.inject.Named;
import java.util.List;

/**
 * 
 * Customer DAO
 * 
 * @author onlinetechvision.com
 * @since 25 Mar 2012
 * @version 1.0.0
 *
 */

@Named
public class OrderDAO implements IOrderDAO {



    @Autowired
    private OrderRepository orderRepository;
	/**
	 * Add Order
	 * 
	 * @param  Order order
	 */
	@Override
	public void addOrder(Order order) {
        orderRepository.save(order);
	}

	/**
	 * Delete Order
	 * 
	 * @param  Order order
	 */
	@Override
	public void deleteOrder(Order order) {
        orderRepository.delete(order);
	}

	/**
	 * Update Order
	 * 
	 * @param  Order order
	 */
	@Override
	public void updateOrder(Order order) {
        orderRepository.save(order);
	}

	/**
	 * Get Order
	 * 
	 * @param  int Order Id
	 * @return Order 
	 */
	@Override
	public Order getOrderById(Long id) {

       return orderRepository.findOne(id);


	}

	/**
	 * Get Order List
	 * 
	 * @return List - Order list
	 */
	@Override
	public Page getOrders(PageRequest posPagination) {


       Page list = orderRepository.findAll(posPagination);
		return list;
	}

    @Override
    /*
    http://docs.spring.io/spring-data/data-jpa/docs/1.0.x/reference/html/#repositories.custom-behaviour-for-all-repositories
     */
    public List<Order> searchOrders(String query) {
        return orderRepository.findAll();  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public boolean updateOrderStatus(Long orderId, OrderStatus status) {
         orderRepository.updateOrderStatus(status.getId(),orderId);  //To change body of implemented methods use File | Settings | File Templates.

    return true;
    }

    @Override
    public boolean updateShippingStatus(Long orderId, ShippingStatus status) {
        orderRepository.updateOrderStatus(status.getId(),orderId);

        return true;  //To change body of implemented methods use File | Settings | File Templates.
    }

}
